package com.kucun.controller;

import java.awt.*;  
import java.awt.geom.*;  
import java.awt.image.*;  
import java.io.*;  
import java.util.*;  
  
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpSession;  
import javax.imageio.ImageIO;

@WebServlet(name="verify",urlPatterns="/verify.do")
public class VerifyCodeAction extends HttpServlet{

	private static final long serialVersionUID = 1L;
	public static String sRand="";
	  
  
    public void init() throws ServletException {  
        super.init();  
    }  
    public Color getRandColor(int s,int e){  
        Random random=new Random ();  
        if(s>255) s=255;  
        if(e>255) e=255;  
        int r,g,b;  
        r=s+random.nextInt(e-s);    
        g=s+random.nextInt(e-s);    
        b=s+random.nextInt(e-s);      
        return new Color(r,g,b);  
    }  
  
    @Override  
    public void service(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException { 
    	String type = request.getParameter("type");
    	
    	if("1".equals(type)){
    		inputCode(request, response);
    	}
    	if("2".equals(type)){
    		getCode(response);
    	}  
         
    }
	public void getCode(HttpServletResponse response) throws IOException {
		PrintWriter pw = response.getWriter();
		pw.write(sRand);
	}
	public void inputCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
		sRand=""; 
        response.setHeader("Pragma", "No-cache");  
        response.setHeader("Cache-Control", "No-cache");  
        response.setDateHeader("Expires", 0);  

        response.setContentType("image/jpeg");  
        int width=86,height=22;     
        BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); //����BufferedImage����,�������൱��һͼƬ  
        Graphics g=image.getGraphics();     
        Random random=new Random();  
        Font mfont=new Font("����",Font.BOLD,16); 
        g.setColor(getRandColor(200,250));  
        g.fillRect(0, 0, width, height);    
        g.setFont(mfont);                   
        g.setColor(getRandColor(180,200));  
          
        
          
        String ctmp="";  
        int itmp=0;  

        for(int i=0;i<4;i++){  
            switch(random.nextInt(3)){  
                case 1:   
                     itmp=random.nextInt(26)+97;  
                     ctmp=String.valueOf((char)itmp);  
                     break;  
                  
                default:  
                     itmp=random.nextInt(10)+48;  
                     ctmp=String.valueOf((char)itmp);  
                     break;  
            }  
            sRand+=ctmp;
            Color color=new Color(20+random.nextInt(110),20+random.nextInt(110),random.nextInt(110));  
            g.setColor(color);  
            g.drawString(ctmp, 15*i+18, 16);  
        }  
        System.out.println(sRand);
        g.dispose();   
        ImageIO.write(image,"JPEG",response.getOutputStream());  
	} 

    public void destroy() {  
        super.destroy();   
    }
}
